<?php

// 上传
// POST common.upload

namespace app\api\resource\common;

class Upload
{
    // 方法
    public $_method = ['POST'];
    // 前置
    public $_pre    = [];
    // 描述
    public $_description = '上传';
    // 参数
    public $_param  = [
        'name'     => '上传控件名称',
        'dir'      => '存储目录,相对应public目录的文件名,默认upload',
        'document' => '上传文件,可选,默认上传图片',
        'editor'   => '编辑器上传,可选',
    ];
    // 返回
    public $_return = [
        201 => '上传成功',
        422 => '参数不正确',
        400 => '上传异常',
    ];

    public function run(&$request)
    {
        $params['name'] = $request->param('name/s','img','trim');
        $params['dir']  = $request->param('dir/s','upload','trim');
        $params['file'] = $request->file($params['name']);
        $params['document'] = $request->param('document',null);

        if ( !empty($params['document']) ) {
            // 上传文件
            $rule = [
                'name' => 'require',
                'dir'  => 'require',
                'file' => 'require|fileExt:pdf,doc,docx,ppt,pptx|fileSize:8388608',
            ];
            $msg = [
                'name.require'  => '名称必须',
                'dir.require'   => '目录必须',
                'file.require'  => '文件必须',
                'file.fileExt'  => '文件后缀必须为[pdf|doc|docx|ppt|pptx]',
                'file.fileSize' => '文件大小必须小于[8M]',
            ];
        } else {
            // 上传图片
            $rule = [
                'name' => 'require',
                'dir'  => 'require',
                // 'file' => 'require|image|fileExt:png,jpg,gif,jpeg|fileSize:2097152',
                'file' => 'require|image|fileExt:png,jpg,gif,jpeg|fileSize:4194304',
            ];
            $msg = [
                'name.require'  => '名称必须',
                'dir.require'   => '目录必须',
                'file.require'  => '文件必须',
                'file.image'    => '文件必须为图片',
                'file.fileExt'  => '文件后缀必须为[png|jpg|gif|jpeg]',
                // 'file.fileSize' => '文件大小必须小于[2M]',
                'file.fileSize' => '文件大小必须小于[4M]',
            ];
        }

        $validate = new \think\Validate($rule,$msg);
        if (!$validate->check($params)) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $uri  = ROOT_PATH . 'public' . DS . $params['dir'];
        if ( !file_exists($uri) ) {
            mkdir($uri,0755);
        }

        $file = $params['file'];
        $info = $file->move($uri);
        if ($info) {
            $uri = '/public/' . $params['dir'] . '/' . date('Ymd') . '/' . $info->getFilename();
            if ( isset($params['editor']) ) {
                header('HTTP/1.1 200 Ok');
                header('Content-Type:application/json;charset=utf-8');
                echo json_encode(['errno'=>0,'data'=>[$uri]], JSON_UNESCAPED_UNICODE);
                exit;
            }
            return [201,['uri'=>$uri]];
        } else {
            $errMsg = $file->getError();
            return [400,$errMsg];
        }
    }
}
